﻿using System.Collections.Generic;
using XFControls.Utils;
using XFControls.XFPanels.XFPanelItems;
using System.Drawing;
using XFControls.XFPanels;
using XFControls;
using System.Windows.Forms;
using System.IO;

namespace XFSenseUI.XFItems
{
    public class SenseItemFileBrowser : SenseItemBase
    {
        public string Filter { get; set; }

        private string _path;
        private List<MenuItem> _contextMenu;
        private int _height;

        public SenseItemFileBrowser()
        {
            SetupStyle();
            _path = "\\";
        }

        public SenseItemFileBrowser(string directory)
        {
            SetupStyle();
            _path = directory;
        }

        public SenseItemFileBrowser(DirectoryInfo info)
        {
            SetupStyle();
            _path = info.FullName;
        }

        private void SetupStyle()
        {
            _contextMenu = new List<MenuItem>();
            ItemType = XFItemType.Clickable;
            Style = new XFItemStyle
                    {
                BorderBottomColor = Color.FromArgb(189, 182, 189),
                DashStyleBottom = System.Drawing.Drawing2D.DashStyle.Dash,
                TextColor = Color.Black,
                TextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                SelectedTextFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular),
                SecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular),
                SelectedSecondaryTextFont = new Font(FontFamily.GenericSansSerif, 7, FontStyle.Regular),
                SelectedBackgroundImageString = "item-select-background",
                SecondaryTextColor = Color.FromArgb(57, 52, 57),
                SelectedTextColor = Color.White,
                SelectedBackgroundColor = Color.FromArgb(43, 36, 43),
                SelectedSecondaryTextColor = Color.FromArgb(182, 178, 182),
                Padding = 11,
                PaddingBottom = 12,
                PaddingLeft = 10,
                PaddingRight = 16
            };
        }

        public override XFItemClickResult GetClickResult()
        {
            OnClick();

            SenseFileBrowserPanel panel = new SenseFileBrowserPanel();
            panel.Attributes = Parent.Attributes;
            panel.ShowScrollbar(true);
            panel.ScrollbarColor = Parent.ScrollbarColor;

            panel.Filter = Filter;
            panel.Selected += (o, e) =>
            {
                SecondaryText = e.Text;
                _path = e.Text.Substring(0, e.Text.LastIndexOf('\\'));

                RestoreMenuItems();
                ScreenReturn();

                OnClick();
            };

            panel = panel.SetStartPath(_path);
            Parent.SavePanelAfterSlide = true;
            Parent.SetRightPanel(panel);
            if (Parent.Parent != null && Parent.Parent is XFPanelContainer)
            {
                var cont = Parent.Parent as XFPanelContainer;

                if (Directory.Exists(_path) && _path != "\\")
                    cont.SetHeaderLeftButton(new XFButtonBack());
                else
                    cont.SetHeaderLeftButton(null);
            }

            ReplaceMenuItems(panel);
            return XFItemClickResult.SlideLeft;
        }

        private void ScreenReturn()
        {
            if (Parent.Parent == null || !(Parent.Parent is XFPanelContainer)) return;
            var cont = Parent.Parent as XFPanelContainer;

            cont.SetHeaderLeftButton(Parent.HasBack ? new XFButtonBack() : null);
        }

        private void RestoreMenuItems()
        {
            var list = Parent as XFPanelList;
            if (list == null || list.Parent == null)
                return;

            var container = list.Parent;
            if (container.Parent == null || !(container.Parent is Form))
                return;

            var parent = container.Parent as Form;
            parent.Menu.MenuItems.Clear();
   
            foreach (MenuItem item in _contextMenu)
            {
                parent.Menu.MenuItems.Add(item);
            }
            ScreenReturn();
        }

        private void ReplaceMenuItems(SenseFileBrowserPanel panel)
        {
            var list = Parent as XFPanelList;
            if (list == null || list.Parent == null)
                return;

            var container = list.Parent;
            if (container.Parent == null || !(container.Parent is Form))
                return;

            var parent = container.Parent as Form;

            _contextMenu.Clear();
            foreach (MenuItem item in parent.Menu.MenuItems)
            {
                _contextMenu.Add(item);
            }
            parent.Menu.MenuItems.Clear();

            var close = new MenuItem
            {
                Text = "Close"
            };

            close.Click += (o, e) =>
            {
                panel.Close();
                RestoreMenuItems();
            };

            parent.Menu.MenuItems.Add(close);

        }

        public override int GetHeight()
        {
            if (!Visible)
                return 0;

            if (_height > 0)
                return _height;

            _height = CalcRecs(0);

            if (Buffer == null)
                Buffer = XFBitmapFactory.GetBitmap(GetWidth(false), _height);

            return _height;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            base.ItemPaint(g, x, y);

            Font fontMain = getCurrentFont(false);
            Font fontSecondary = getCurrentFont(true);
            Color colorMain = getCurrentFontColor(false);
            Color colorSecondary = getCurrentFontColor(true);


            if (!string.IsNullOrEmpty(MainText))
                g.DrawString(MainText, _mRec, Style, fontMain, colorMain);
            if (!string.IsNullOrEmpty(SecondaryText))
                g.DrawString(SecondaryText, _sRec, Style, fontSecondary, colorSecondary);

            if (!string.IsNullOrEmpty(Icon))
                DrawUI(Icon, g, new Rectangle(Style.PaddingLeft, Style.PaddingTop, _iconSize, _iconSize));
        }
    }
}
